<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    //
    use SoftDeletes;

    protected $casts=[
        'basic_info'=>'json',
        'tag_ids'=>'json'
    ];

    protected $dates=['deleted_at'];

    protected $fillable=['id','name','basic_info','apply_condition','apply_material','apply_tip','tag_ids','is_hot','status','apply_num'];

    //获取basic_info信息
    public function getBasicInfoAttribute($value){
        $arr=json_decode($value,true);
        $arr['agent_name']='';
        $arr['category_name_first']='';
        $arr['category_name_second']='';
        $arr['loan_start']=isset($arr['loan_start'])?$arr['loan_start']:'';
        $arr['loan_end']=isset($arr['loan_end'])?$arr['loan_end']:'';
        $arr['age_start']=isset($arr['age_start'])?$arr['age_start']:'';
        $arr['age_end']=isset($arr['age_end'])?$arr['age_end']:'';
        $arr['repay_start']=isset($arr['repay_start'])?$arr['repay_start']:'';
        $arr['repay_end']=isset($arr['repay_end'])?$arr['repay_end']:'';
        $arr['progress_start']=isset($arr['progress_start'])?$arr['progress_start']:'';
        $arr['progress_end']=isset($arr['progress_end'])?$arr['progress_end']:'';
        $arr['rate_start']=isset($arr['rate_start'])?$arr['rate_start']:'';
        $arr['rate_end']=isset($arr['rate_end'])?$arr['rate_end']:'';
        $arr['require_white']=isset($arr['require_white'])?$arr['require_white']:'';
        $arr['require_workplace']=isset($arr['require_workplace'])?$arr['require_workplace']:'';
        $arr['require_house']=isset($arr['require_house'])?$arr['require_house']:'';
        $arr['require_live']=isset($arr['require_live'])?$arr['require_live']:'';
        $arr['area']=isset($arr['area'])?$arr['area']:'';

        if(array_key_exists('agent_id',$arr)){
            $agent=Agent::find($arr['agent_id']);
            if($agent){
                $arr['agent_name']=$agent->name;
            }
        }
        if(array_key_exists('category_id_first',$arr)){
            $dictionary=Dictionary::find($arr['category_id_first']);
            if($dictionary){
                $arr['category_name_first']=$dictionary->name;
            }
        }
        if(array_key_exists('category_id_second',$arr)){
            $dictionary=Dictionary::find($arr['category_id_second']);
            if($dictionary){
                $arr['category_name_second']=$dictionary->name;
            }
        }
        if(array_key_exists('repay_type',$arr)){
            $dictionary=Dictionary::find($arr['repay_type']);
            if($dictionary){
                $arr['repay_type_name']=$dictionary->name;
            }
        }
        return $arr;
    }

    public function setBasicInfoAttribute($arr){

        if(array_key_exists('loan_start',$arr)){
            $arr['loan_start']=floatval($arr['loan_start']);
        }
        if(array_key_exists('loan_end',$arr)){
            $arr['loan_end']=floatval($arr['loan_end']);
        }
        if(array_key_exists('age_start',$arr)){
            $arr['age_start']=intval($arr['age_start']);
        }
        if(array_key_exists('age_end',$arr)){
            $arr['age_end']=intval($arr['age_end']);
        }
        if(array_key_exists('repay_start',$arr)){
            $arr['repay_start']=intval($arr['repay_start']);
        }
        if(array_key_exists('repay_end',$arr)){
            $arr['repay_end']=intval($arr['repay_end']);
        }
        if(array_key_exists('rate_start',$arr)){
            $arr['rate_start']=floatval($arr['rate_start']);
        }
        if(array_key_exists('rate_end',$arr)){
            $arr['rate_end']=floatval($arr['rate_end']);
        }
        if(array_key_exists('progress_start',$arr)){
            $arr['progress_start']=intval($arr['progress_start']);
        }
        if(array_key_exists('progress_end',$arr)){
            $arr['progress_end']=intval($arr['progress_end']);
        }
        $this->attributes['basic_info'] =json_encode($arr);
    }
}
